﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Web.Mvc;
using Kendo.Mvc;
using Kendo.Mvc.Infrastructure;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using SecurityApi.DataAccess;

namespace SecurityAdmin.Web.Controllers
{
    public class RoleController : BaseController
    {
        //
        // GET: /Role/

        public ActionResult GetRoles()
        {
            return View();
        }

        public ActionResult AddNewRole()
        {
            Role newRole = new Role();
            return View(newRole);
        }

        [HttpPost]
        public ActionResult AddNewRole(Role newRole)
        {
            RoleRepository repository = new RoleRepository();
            int roleId = repository.AddRole(newRole.RoleName, SelectedApplication.ApplicationId);
            if (roleId != -1)
            {
                return RedirectToAction("GetRoles");
            }

            return View(newRole);
        }

        public ActionResult RemoveRole(int roleId)
        {
            RoleRepository repository = new RoleRepository();
            bool bResult= repository.RemoveRole(roleId, SelectedApplication.ApplicationId);
            return RedirectToAction("GetRoles");
        }



        public ActionResult AjaxGetApplicationRoles(int applicationId,[DataSourceRequest] DataSourceRequest request)
        {
            SecurityApi.DataAccess.RoleRepository repository = new SecurityApi.DataAccess.RoleRepository();
            IQueryable<Role> users = repository.GetApplicationRoles(applicationId);
            return Json(users.ToDataSourceResult(request));
        }

    }
}
